package com.changdu.seatunnel.admin.config;

import java.io.IOException;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.changdu.seatunnel.admin.config.common.RequestContext;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;

/**
 * @author wangxiaoliang
 * @date   2022年5月2日
 */
public class AccessTokenFilterFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
    	try {
    		HttpServletRequest req = (HttpServletRequest) request;
    		String accesstoken = req.getHeader("accesstoken");
    		if (StringUtils.isNotBlank(accesstoken)) {
    			RequestContext context = new RequestContext();
    			context.setAccessToken(accesstoken);
    			RequestContext.set(context);
    		}
    		chain.doFilter(request, response);
		} finally {
			RequestContext.remove();
		}
    }

	@Override
    public void init(FilterConfig filterConfig) {}
    
    @Override
    public void destroy() {}
    
}